import { isPhoneNumber } from '@/utils/index'

export const formRules = {
  name: [{ required: true, validator: notEmpty, msg: '请输入单位名称', trigger: ['change','blur'] }],
  type: [{ required: true, validator: notEmpty, msg: '请选择单位类型', trigger: ['change','blur'] }],
  creditCode: [{ required: true, validator: notEmpty, msg: '请输入社会统一信用代码', trigger: ['change','blur'] }],
  contactPerson: [{ required: true, validator: notEmpty, msg: '请输入联系人', trigger: ['change','blur'] }],
  phone: [{ required: true, validator: notEmpty, msg: '请输入联系电话', trigger: ['change','blur'] }],
}



// 校验不能为空
function notEmpty(rule, value, callback) {
  if (!value) {
    return callback(new Error(rule.msg))
  }else if (rule.field === 'phone') {
    phoneRules(rule, value, callback)
  }else{
    return callback()
  }
}

function phoneRules(rule, value, callback) {
  if (!isPhoneNumber(value)) {
    return callback(new Error('请输入正确的手机号'))
  }else{
    callback()
  }
}